package com.lyz.ejb.demo;


import com.lyz.ejb.entity.PartyGroup;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @author chenjx
 *
 */
@Stateless//无状态连接
@Remote(RemoteHelloWorld.class)//格式：接口.class
public class HelloWorldBean implements RemoteHelloWorld {

    //private static Logger log = LoggerFactory.getLogger(HelloWorldBean.class);

    @PersistenceContext
    private EntityManager em;

    public String sayHello(String name) {
        String str = "Hello " + name;
        System.out.println(str);//这个在服务端输出
        return str;//这个返回给调用该服务方法的客户端
    }

    @Override
    public String getGroupIdByName(String groupName) {

        if(StringUtil.isEmpty(groupName)){
            return "null";
        }

        String sql = "from PartyGroup p where p.groupName=:groupName";

        Query query = em.createQuery(sql).setParameter("groupName",groupName);
        List<PartyGroup> result = query.getResultList();

        if (null != result && result.size() > 0) {
            PartyGroup obj =  result.get(0);
            return obj.getPartyId();
        }

        return null;
    }

}